{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "835af266-a7a4-4283-9557-5e06681b3b28",
   "metadata": {
    "id": "X2DqArfaJZvx"
   },
   "source": [
    "# Using Function Calling For Structure"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc2ecabf-cd7b-44bf-ae4f-26d75e8dd037",
   "metadata": {
    "id": "-wEri7mHQ7d8"
   },
   "source": [
    "## Simple Example Of Address Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "344e22ed-9b97-4571-a77f-00dcbbb4189c",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "height": 98,
    "id": "OEXEffrCGgft",
    "outputId": "42cf9fed-9d21-4467-d8fa-aa58dcdf055e"
   },
   "outputs": [],
   "source": [
    "text = \\\n",
    "\"\"\"\n",
    "John Doe lives at 123 Elm Street, Springfield. Next to him is Jane Smith, residing at 456 Oak Avenue, Lakeview. Not far away, we find Dr. Emily Ryan at 789 Pine Road, Westwood. Meanwhile, in a different part of town, Mr. Alan Turing can be found at 101 Binary Blvd, Computerville. Nearby, Ms. Olivia Newton stays at 202 Music Lane, Harmony. Also, Prof. Charles Xavier is located at 505 Mutant Circle, X-Town.\n",
    "\"\"\"\n",
    "print (text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e58220bb-b863-4a09-8b4b-4f790ee87206",
   "metadata": {
    "height": 183,
    "id": "q7ARknmXL5uw"
   },
   "outputs": [],
   "source": [
    "raven_prompt = \\\n",
    "f'''\n",
    "Function:\n",
    "def address_name_pairs(names : list[str], addresses : list[str]):\n",
    "\"\"\"\n",
    "Give names and associated addresses.\n",
    "\"\"\"\n",
    "\n",
    "{text}<human_end>\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "839d2743-0074-42e7-a792-62f6a396fdec",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "height": 200,
    "id": "7G55J8G2MLA5",
    "outputId": "a36f5487-1714-4974-a0ec-e71e83f39e0f"
   },
   "outputs": [],
   "source": [
    "from utils import query_raven\n",
    "\n",
    "def address_name_pairs(names : list[str], addresses : list[str]):\n",
    "  \"\"\"\n",
    "  Give names and associated addresses.\n",
    "  \"\"\"\n",
    "  for name, addr in zip(names, addresses):\n",
    "    print (name, \": \", addr)\n",
    "\n",
    "result = query_raven(raven_prompt)\n",
    "eval(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d38143b-ba80-4b65-9b55-ed65f4fb8d48",
   "metadata": {},
   "source": [
    "## Alternative Way of Doing Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f53d2535-7d34-4ef5-ad70-69a324a888ca",
   "metadata": {
    "height": 98
   },
   "outputs": [],
   "source": [
    "unbalanced_text = \\\n",
    "\"\"\"\n",
    "Dr. Susan Hill has a practice at 120 Green Road, Evergreen City, and also consults at 450 Riverdale Drive, Brookside. Mark Twain, the renowned author, once lived at 300 Maple Street, Springfield, but now resides at 200 Writers Block, Literaryville. The famous artist, Emily Carter, showcases her work at 789 Artisan Alley, Paintown, and has a studio at 101 Palette Place, Creativeland. Meanwhile, the tech innovator, John Tech, has his main office at 555 Silicon Street, Techville, and a secondary office at 777 Data Drive, Computown, but he lives at 123 Digital Domain, Innovatown.\n",
    "\"\"\"\n",
    "print (unbalanced_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4adef284-c7f2-400f-8bc0-0a4ab3fab238",
   "metadata": {
    "height": 370,
    "tags": []
   },
   "outputs": [],
   "source": [
    "raven_prompt = \\\n",
    "f'''\n",
    "\n",
    "@dataclass\n",
    "class Record:\n",
    "    name : str\n",
    "    addresses : List[str]\n",
    "\n",
    "Function:\n",
    "def insert_into_database(names : List[Record]):\n",
    "\"\"\"\n",
    "Inserts the records into the database. \n",
    "\"\"\"\n",
    "\n",
    "{unbalanced_text}<human_end>\n",
    "\n",
    "'''\n",
    "\n",
    "result = query_raven(raven_prompt)\n",
    "print (result)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ca74ee7-d1e1-4ad1-80ff-2625be14246c",
   "metadata": {
    "id": "77-vmPaFakXb"
   },
   "source": [
    "## Generating Valid JSONs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7234d7a6-356d-4468-a4a7-86ddfe89dfb0",
   "metadata": {},
   "source": [
    "```\n",
    "{\n",
    "  \"city_name\" : \"London\"\n",
    "  \"location\" : {\n",
    "      \"country\" : \"United Kingdom\",\n",
    "      \"continent\" : {\n",
    "          \"simple_name\" : \"Europe\",\n",
    "          \"other_name\" : \"Afro-Eur-Asia\"\n",
    "      }\n",
    "  }\n",
    "}\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edd8ceb8-7b12-4240-ac4b-c5449618639f",
   "metadata": {
    "height": 268
   },
   "outputs": [],
   "source": [
    "def city_info(city_name : str, location : dict):\n",
    "  \"\"\"\n",
    "  Gets the city info\n",
    "  \"\"\"\n",
    "  return locals()\n",
    "def construct_location_dict(country : str, continent : dict):\n",
    "  \"\"\"\n",
    "  Provides the location dictionary\n",
    "  \"\"\"\n",
    "  return locals()\n",
    "def construct_continent_dict(simple_name : str, other_name : str):\n",
    "  \"\"\"\n",
    "  Provides the continent dict\n",
    "  \"\"\"\n",
    "  return locals()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31f84b28-32ad-4db1-a9dd-b6de6c3b731c",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "print (city_info(\"London\", {}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a9c33ac-045a-43ac-8e1a-68f4a2b4d514",
   "metadata": {
    "height": 370,
    "id": "z09ke72Mao82"
   },
   "outputs": [],
   "source": [
    "raven_prompt = \\\n",
    "'''\n",
    "Function:\n",
    "def city_info(city_name : str, location : dict):\n",
    "\"\"\"\n",
    "Gets the city info\n",
    "\"\"\"\n",
    "\n",
    "Function:\n",
    "def construct_location_dict(country : str, continent : dict):\n",
    "\"\"\"\n",
    "Provides the location dictionary\n",
    "\"\"\"\n",
    "\n",
    "def construct_continent_dict(simple_name : str, other_name : str):\n",
    "\"\"\"\n",
    "Provides the continent dict\n",
    "\"\"\"\n",
    "\n",
    "User Query: {question}<human_end>\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10c9c60b-19b0-4dbb-8a9c-2dc92c5aad05",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "height": 115,
    "id": "bM-xNHKDam9V",
    "outputId": "962eea9e-c029-46cf-a945-f84dc8b411d8"
   },
   "outputs": [],
   "source": [
    "question = \"I want the city info for London, \"\\\n",
    "\"which is in the United Kingdom, which is in Europe or Afro-Eur-Asia.\"\n",
    "\n",
    "output = query_raven(raven_prompt.format(question = question))\n",
    "json0 = eval(output)\n",
    "print (json0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9238acd7-388b-4f8c-aed9-f86cbf7f73c4",
   "metadata": {
    "height": 47
   },
   "outputs": [],
   "source": [
    "import json\n",
    "json.dumps(json0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5dd2bf6a-b6cc-44e6-a20e-6f569e8d7577",
   "metadata": {},
   "source": [
    "### Try These yourself!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09c43a37-6bf3-453b-b263-e53ab2d6856a",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "height": 132,
    "id": "Wh7kacTdd_Nb",
    "outputId": "b3475d87-7efa-4e69-e7dd-ddd821fc40a8"
   },
   "outputs": [],
   "source": [
    "question = \"I need details for the city of Tokyo, \"\\\n",
    "\"situated in Japan, a part of the Asian continent, \"\\\n",
    "\"which is sometimes referred to as Eurasia.\"\n",
    "\n",
    "output = query_raven(raven_prompt.format(question = question))\n",
    "json1 = eval(output)\n",
    "print (json1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2901c617-9688-45e1-b6ae-06c6a8ac01af",
   "metadata": {
    "height": 47
   },
   "outputs": [],
   "source": [
    "import json\n",
    "json.dumps(json0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69903cb2-b003-4c51-84b9-a5bf70a911b2",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
